import Request from 'utils/request';

import Advertisement from 'models/homePage/advertisement';
import RecommendGood from 'models/homePage/recommendGood';
import Goods from 'models/homePage/goods';
import SysNotice from 'models/homePage/sysNotice';

export default class HomePageApi {
  static getAdvertiseData() {
    const apiName = 'api/wehcatshop/getAdvertisement';

    return Request.post(apiName, {
      advertisementStatus: 1
    }).then(res => {
      let advertisement = new Advertisement(res.data);
      return advertisement;
    });
  }

  static getRecommendGoods(location, pagination, buyMethod) {
    const apiName = 'api/wehcatshop/letBuy';

    let lnglat = location.lnglat;
    let postData = {
      buyMethod,
      latitude: lnglat.lat,
      longitude: lnglat.lng,
      limit: pagination.pageSize,
      page: pagination.currentPage
    };

    return Request.post(apiName, postData).then(res => {
      let recommendGoods = res.data;
      if (recommendGoods === null) {
        recommendGoods = [];
      }
      let goodsList = [];
      for (let i = 0; i < recommendGoods.length; i++) {
        let goods = recommendGoods[i];
        goods = new RecommendGood(goods);
        goodsList.push(goods);
      }

      pagination.totalCount = res.rows;
      return goodsList;
    });
  }

  static getGoodsList(location, pagination, buyMethod) {
    const apiName = 'api/wehcatshop/goodsList';

    let lnglat = location.lnglat;
    let postData = {
      buyMethod,
      latitude: lnglat.lat,
      longitude: lnglat.lng,
      limit: pagination.pageSize,
      page: pagination.currentPage
    };

    return Request.post(apiName, postData).then(res => {
      let goodsList = res.data;
      if (goodsList === null) {
        goodsList = [];
      }
      let goodses = [];
      for (let i = 0; i < goodsList.length; i++) {
        let obj = goodsList[i];
        obj = new Goods(obj);
        goodses.push(obj);
      }

      pagination.totalCount = res.rows;
      return goodses;
    });
  }

  static getHomeClassifyType() {
    const apiName = 'api/wehcatshop/getWechatFuncControllerList';

    return Request.post(apiName, {
      type:1
    }, true, false).then(res => {
      let typeList = res.data;
      typeList = typeList.map(obj => obj.id);
      return typeList;
    });
  }

  static getNoticeList(pagination) {
    const apiName = 'api/wehcatshop/noticeTipList';
    return Request.post(apiName, {
      limit: pagination.pageSize,
      page: pagination.currentPage
    }).then(res => {
      let noticeList = res.data;
      let notices = [];
      for (let i = 0; i < noticeList.length; i++) {
        let notice = noticeList[i];
        notice = new SysNotice(notice);
        notices.push(notice);
      }

      pagination.totalCount = res.rows;
      return notices;
    });
  }

  static toLookNotice(id) {
    const apiName = 'api/wehcatshop/noticeTipLooked';

    return Request.post(apiName, {
      noticeTipId: id
    }).then(res => {
      return res.msg;
    });
  }

  static getNoticeById(id) {
    const apiName = 'api/wehcatshop/oneNiticeTipById';

    return Request.post(apiName, {
      noticeTipId: id
    }).then(res => {
      let notice = res.data;
      notice = new SysNotice(notice);
      return notice;
    });
  }
}
